﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class MessageCenter
{
    public static MessageCenter Instance = new MessageCenter();
    private Dictionary<string, List<Action<object>>> m_Actions = new Dictionary<string, List<Action<object>>>();

    public void SendMessage(string message, object param = null)
    {
        if (m_Actions.ContainsKey(message))
        {
            var list = m_Actions[message];
            if (list != null)
            {
                foreach (var action in list)
                {
                    if (action != null)
                    {
                        action(param);
                    }
                }
            }
        }
    }

    public void Register(string message, Action<object> action)
    {
        if (!m_Actions.ContainsKey(message))
        {
            m_Actions[message] = new List<Action<object>>();
        }
        var list = m_Actions[message];
        if (!list.Contains(action))
        {
            list.Add(action);
        }
    }

    public void UnRegister(string message, Action<object> action)
    {
        if (!m_Actions.ContainsKey(message))
        {
            return;
        }
        var list = m_Actions[message];
        if (list.Contains(action))
        {
            list.Remove(action);
        }
    }
}

